/**
 * @requires libs/json2
 * @requires libs/msgs
 */
define([], function() {

    /**
     * Form module
     *
     * @exports modules/form
     * @version {version}
     */

    /**
     * Form class
     * @class Nav
     * @return {Nav} An object instance
     */

    var Nav = base.Module.extend({
        option:{
            loadLocation:'.content',
            submitType: 'json',
            responseType: 'json',
            submitMethod: 'POST',
        },
        init: function(opts, element) {
            this._super(opts, element);
            this.navForm = $('.nav ');
            this.filterNav = $(this.navForm).find(".filters");
            this.filters = $(this.filterNav).find("option");
            this.filter = $(this.filterNav).find("option:selected");
            this._build();
        },
        _build:function(){

            $(this.filterNav).on('change', $.proxy(function(e){
                e.preventDefault();
                this.filter = $(this.filterNav).find("option:selected");
                if($("#content",this.el).val() == "" && $(this.filter).val() == "" ){
                    $(this.navForm).find("#filter").val("");
                }else{
                    $(this.navForm).find("#filter").val($(this.filter).val());
                }
            }, this));

            $(".btn-search", this.el).on("click", $.proxy(function(e){
                e.preventDefault();

                this.submitForm();
            },this));

            $("#content",this.el).keyup(function(event){
                if($(this).val() == ""){
                    $(".btn-search").trigger("click");
                }
            });
            $("#content",this.el).keydown($.proxy(function(event){
                if(event.keyCode == 13) {
                  event.preventDefault();
                  $(".btn-search", this.el).trigger("click");
                  return false;
                }
            }, this));

            this.keyPressHandler();
        },
        /**
         *
         */
        serializeForm: function() {
            var formJson = {};
            var formArr = $($(this.navForm).find("form")[0]).serializeArray();

            for (var item in formArr) {
                if (formArr.hasOwnProperty(item)){
                    var formItem = formArr[item];
                    //hack for grouped items
                    formName = (formItem.name.split('['));
                    if (formName.length > 1) {
                        if (formJson[formName[0]] === undefined) {
                            formJson[formName[0]] = {};
                        }
                        formJson[formName[0]][formName[1].split(']')[0]] = formItem.value;
                    } else {
                        formJson[formItem.name] = formItem.value;
                    }
                }
            }
            return formJson;
        },
        keyPressHandler: function(){
            //Press Enter in INPUT moves cursor to next INPUT
            $("#content", this.el).keypress(function(e){
               
                if ( e.which == 13) // Enter key = keycode 13
                {
                    e.preventDefault();
                    $(".btn-search").trigger("click");
                }
            });
        },
        submitForm : function(){
            this.formData = JSON.stringify(this.serializeForm());

            $(".content", this.el).html($('<span class="loader"></span>'));
            // submit via ajax
            $.ajax({
                url: $($(this.navForm).find("form")[0]).prop("action"),
                type: "POST",
                data: this.formData,
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                cache: false,
                complete:function(data){

                    if(data.statusText == "OK"){
                        $(".loader", this.el).remove();
                        $(".content", this.el).html(data.responseText);
                    }
                }
            });
        }
    });

    base.plugin('nav', Nav, '.nav');

    return Nav;
});